package com.ocom.payconfig.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ocom.common.entity.payconfig.EntityTimingtaskLog;
import com.ocom.common.enums.DataSourceEnum;
import com.ocom.common.enums.MasterSlaveEnum;
import com.ocom.mysql.aspect.DataSourceAspect;
import com.ocom.payconfig.dao.IDaoTimingtaskLog;
import com.ocom.payconfig.service.TimingtaskLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class TimingtaskLogServiceImpl extends ServiceImpl<IDaoTimingtaskLog, EntityTimingtaskLog> implements TimingtaskLogService {


    @Autowired
    private IDaoTimingtaskLog iDaoTimingtaskLog;

    @DataSourceAspect(type= DataSourceEnum.MAIN_DB,isMaster = MasterSlaveEnum.SLAVE)
    @Override
    public EntityTimingtaskLog selectTimingtaskLogs(Long comId, String mon) {
        QueryWrapper<EntityTimingtaskLog> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("task_name","account_balance");
        queryWrapper.eq("com_id",comId);
        queryWrapper.eq("str_key_1",mon);
        queryWrapper.last("limit 1");
        return iDaoTimingtaskLog.selectOne(queryWrapper);
    }

    @DataSourceAspect(type= DataSourceEnum.MAIN_DB,isMaster = MasterSlaveEnum.MASTER)
    @Override
    public Boolean saveTimingtaskLog(Long comId, int logId, String mon) {
        if (logId==0){
            EntityTimingtaskLog entityTimingtaskLog=new EntityTimingtaskLog();
            entityTimingtaskLog.setTaskName("account_balance");
            entityTimingtaskLog.setComId(Math.toIntExact(comId));
            entityTimingtaskLog.setStrKey1(mon);
            entityTimingtaskLog.setStatus((byte) 1);
            entityTimingtaskLog.setEndTime(new Date());
            return this.save(entityTimingtaskLog);
        }else {
            UpdateWrapper<EntityTimingtaskLog> updateWrapper=new UpdateWrapper<>();
            updateWrapper.set("task_name","account_balance");
            updateWrapper.set("com_id",comId);
            updateWrapper.set("str_key_1",mon);
            updateWrapper.set("status",1);
            updateWrapper.set("end_time",new Date());
            updateWrapper.eq("id",logId);
            return this.update(updateWrapper);
        }
    }
}
